import type BpmnControlService from '../../controls/bpmn/service';
import { type ICardView } from '../base';
import CardService from '../base/service';

class FormCardService<T> extends CardService implements ICardView {
  controls: Array<BpmnControlService<T>>;

  constructor({ title, controls }: { title: string; controls: Array<BpmnControlService<T>> }) {
    super({ title, controls });
    this.controls = controls;
  }

  init(): void {
    super.init();
    this.initEvent();
  }

  updateProperties() {
    const element = this.panel.getElement();
    // 只支持外置表单
    this.root.getModeling().updateProperties(
      element,
      {
        'flowable:formType': 'external',
      },
    );
  }

  initEvent() {
    this.controls.forEach(it => {
      it.bindOnChange(() => {
        this.updateProperties();
        it.updateProperties(it.value as T);
      });
    });
  }
}

export default FormCardService;
